An AI-powered digital twin application that combines a Next.js frontend with a FastAPI backend, deployed on AWS using Terraform. The system leverages AWS Bedrock's Amazon Nova models for intelligent conversations with persistent session memory.
Digital Twin is a full-stack application that provides an AI course companion capable of:
- Real-time chat interactions with context-aware responses
- Session-based conversation memory (stored in S3)
- Lambda-optimized backend deployment
- Dual backend support: Traditional FastAPI + AWS Bedrock Agentcore
- Responsive, modern UI with TypeScript and Tailwind CSS
- Multi-environment support (dev, test, prod)
This project supports two backend implementations running in parallel:
1. Traditional Backend (/chat endpoint)
Frontend β API Gateway β Lambda (FastAPI)
β
Direct Bedrock API
β
Manual S3 Memory
2. Agentcore Backend (/agentcore/chat endpoint) - NEW! π
Frontend β API Gateway β Lambda (FastAPI)
β
Bedrock Agent
β
Built-in Memory Service
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Next.js Frontend (port 3000) β
β React 19.2.1 + TypeScript + Tailwind β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
HTTP/HTTPS API
β
βββββββββββββ΄ββββββββββββ
β β
βββββββββΌβββββββββ ββββββββββΌββββββββββ
β Traditional β β Agentcore β
β Lambda Backend β β Lambda Backend β
β (/chat) β β (/agentcore/*) β
βββββββββ¬βββββββββ ββββββββββ¬ββββββββββ
β β
βββββββββΌβββββββββ ββββββββββΌββββββββββ
β Direct Bedrock β β Bedrock Agent β
β API Calls β β Orchestration β
βββββββββ¬βββββββββ ββββββββββ¬ββββββββββ
β β
βββββββββΌβββββββββ ββββββββββΌββββββββββ
β S3 Memory β β Built-in Memory β
β (Manual) β β (Automatic) β
ββββββββββββββββββ ββββββββββββββββββββ
β β
ββββββββββββ¬ββββββββββββ
β
ββββββββΌβββββββ
β CloudFront β
β Cache β
βββββββββββββββ
twin/
βββ frontend/ # Next.js React application
β βββ app/ # App Router pages
β β βββ layout.tsx # Root layout
β β βββ page.tsx # Home page
β β βββ globals.css # Global styles
β βββ components/
β β βββ twin.tsx # Main chat component
β βββ public/ # Static assets
β βββ package.json # Dependencies
β βββ next.config.ts # Next.js configuration
β
βββ backend/ # FastAPI Python backend
β βββ server.py # Traditional FastAPI app
β βββ lambda_handler.py # Traditional Lambda entry point
β βββ agentcore_server.py # Agentcore FastAPI app (NEW)
β βββ agentcore_lambda_handler.py # Agentcore Lambda entry point (NEW)
β βββ agentcore_config.py # Agent configuration (NEW)
β βββ context.py # AI prompt context
β βββ resources.py # Shared utilities
β βββ deploy.py # Lambda packaging script
β βββ requirements.txt # Python dependencies
β βββ .env.agentcore # Agentcore environment template (NEW)
β βββ data/ # Static data files
β β βββ facts.json # Knowledge base
β β βββ style.txt # Response style guidelines
β β βββ summary.txt # System summary
β βββ lambda-package/ # Packaged Lambda deployment
β
βββ terraform/ # Infrastructure as Code
β βββ main.tf # Traditional backend resources
β βββ agentcore.tf # Agentcore backend resources (NEW)
β βββ agentcore_variables.tf # Agentcore variables (NEW)
β βββ variables.tf # Input variables
β βββ versions.tf # Provider versions
β βββ output.tf # Output values
β βββ terraform.tfvars # Environment-specific vars
β βββ terraform.tfstate.d/ # State storage backends
β
βββ scripts/
β βββ deploy.sh # Main deployment script (Bash)
β βββ deploy.ps1 # Alternative deployment (PowerShell)
β βββ upload_knowledge_base.sh # Prepare Agentcore agent (NEW)
β
βββ memory/ # Local session memory (dev)
β βββ *.json # Session conversation history
β
βββ README.md # This file (start here!)
βββ DOCUMENTATION_INDEX.md # Complete documentation guide (NEW)
βββ DECISION_TREE.md # Choose the right backend (NEW)
βββ BACKEND_COMPARISON.md # Compare Traditional vs Agentcore (NEW)
βββ QUICK_START_AGENTCORE.md # Deploy Agentcore in 3 steps (NEW)
βββ AGENTCORE_MIGRATION_GUIDE.md # Detailed deployment guide (NEW)
βββ AGENTCORE_COST_OPTIMIZED.md # Cost breakdown & optimization (NEW)
βββ CHANGELOG.md # Version history (NEW)
- Node.js 18+ (frontend)
- Python 3.13+ (backend)
- AWS CLI configured with credentials
- Docker (for Lambda package building)
- Terraform 1.0+ (for infrastructure)
- uv (Python package manager, recommended)
cd frontend
npm install
npm run devVisit http://localhost:3000 to view the application.
cd backend
# Create a .env file
cat > .env << EOF
CORS_ORIGINS=http://localhost:3000
BEDROCK_MODEL_ID=amazon.nova-lite-v1:0
DEFAULT_AWS_REGION=us-east-1
USE_S3=false
MEMORY_DIR=../memory
EOF
# Install dependencies
pip install -r requirements.txt
# or with uv:
uv sync
# Run development server
uvicorn server:app --reload --port 8000The API will be available at http://localhost:8000.
Open the frontend, type a message in the chat, and watch it communicate with your local backend.
Frontend (frontend/.env.local):
NEXT_PUBLIC_API_URL=http://localhost:8000Backend (backend/.env):
# AWS Configuration
DEFAULT_AWS_REGION=us-east-1
BEDROCK_MODEL_ID=amazon.nova-lite-v1:0
# CORS
CORS_ORIGINS=http://localhost:3000,https://yourdomain.com
# Storage
USE_S3=false # Set to true for production
S3_BUCKET=twin-memory-bucket
MEMORY_DIR=../memory # Local path for dev
# Bedrock Model Options (pick one):
# amazon.nova-micro-v1:0 (fastest, cheapest)
# amazon.nova-lite-v1:0 (balanced - default)
# amazon.nova-pro-v1:0 (most capable, higher cost)project_name = "twin"
environment = "dev"
default_aws_region = "us-east-1"
use_custom_domain = false
root_domain = ""
bedrock_model_id = "amazon.nova-lite-v1:0"# Deploy to dev environment (includes both backends)
./scripts/deploy.sh dev
# Deploy to production
./scripts/deploy.sh prod twinWhat the deployment does:
- β Packages the backend into a Lambda-compatible ZIP
- β Initializes Terraform state in S3
- β
Creates/updates AWS infrastructure:
- Traditional Backend: Lambda function with FastAPI/Mangum
- Agentcore Backend: Lambda + Bedrock Agent (NEW)
- API Gateway for HTTP routing (both backends)
- S3 bucket for conversation memory
- CloudFront CDN (optional)
- IAM roles and policies
- β Deploys the frontend to CloudFront/S3
- β Outputs API endpoints and frontend URL
- AWS account with appropriate permissions (Lambda, API Gateway, S3, IAM, Bedrock, Bedrock Agents)
- S3 backend bucket for Terraform state:
twin-terraform-state-{ACCOUNT_ID} - AWS Bedrock model access in your region
- AWS Bedrock Agents access (for Agentcore backend)
For detailed Agentcore deployment instructions, see:
- Quick Start:
QUICK_START_AGENTCORE.md - Full Guide:
AGENTCORE_MIGRATION_GUIDE.md - Cost Details:
AGENTCORE_COST_OPTIMIZED.md
# Deploy both backends
cd terraform
terraform apply
# Both endpoints will be available:
# Traditional: https://your-api/chat
# Agentcore: https://your-api/agentcore/chat- Next.js 16.0.7 - React framework with server-side rendering
- React 19.2.1 - UI library
- Tailwind CSS 4 - Utility-first CSS framework
- Lucide React - Icon library
- TypeScript 5 - Type-safe JavaScript
- FastAPI - Modern Python web framework
- Uvicorn - ASGI server
- Boto3 - AWS SDK for Python
- Mangum - ASGI-to-Lambda adapter
- PyPDF - PDF processing (if needed)
- Python Multipart - Multipart form data handling
This project has been patched against the critical React Server Components RCE vulnerability:
- Next.js: 16.0.7 (patched from 16.0.3)
- React: 19.2.1 (patched from 19.2.0)
- React-DOM: 19.2.1 (patched from 19.2.0)
- β CORS configured for specific origins
- β S3 buckets with public access blocked
- β IAM roles with least-privilege permissions
- β Environment variables for sensitive configuration
- β Session IDs validated on each request
cd frontend
npm run lintcd backend
# (Add pytest tests as needed)
pytest tests/Once the backend is running, visit the interactive API documentation (Swagger UI):
- Traditional backend:
http://localhost:8000/docs - Agentcore backend:
http://localhost:8001/docs
POST /chat
- Request:
{ "message": "Tell me about AI", "session_id": "optional-session-uuid" } - Response:
{ "response": "AI is...", "session_id": "generated-or-provided-uuid" }
POST /agentcore/chat
- Request:
{ "message": "Tell me about AI", "session_id": "optional-session-uuid" } - Response:
{ "response": "AI is...", "session_id": "generated-or-provided-uuid", "agent_type": "agentcore", "trace": {...} // Optional trace data }
| Feature | Traditional (/chat) |
Agentcore (/agentcore/chat) |
|---|---|---|
| Memory | Manual S3 management | Automatic (built-in) |
| Context | Loaded at runtime | Embedded in agent |
| Orchestration | Single API call | Multi-agent capable |
| Tracing | Manual logging | Built-in trace data |
| Cost | ~$5-20/month | ~$5-20/month |
- Development (Local): Conversations stored in
memory/directory as JSON files - Production (S3): Sessions saved to S3 bucket (manual management)
- All Environments: Built-in Bedrock Agent memory service (automatic)
- No manual S3 management required
- Session persistence handled by AWS
Both backends are deployed and available. Choose based on your needs:
- β You want proven, stable technology
- β You need full control over the conversation flow
- β You're comfortable with manual memory management
- β You want the simplest possible architecture
- β You want built-in conversation memory
- β You need multi-agent orchestration capabilities
- β You want better tracing and debugging
- β You're building complex agentic workflows
- β You want AWS-native agent management
Both backends have similar costs (~$5-20/month usage-based):
- Traditional: Lambda + Bedrock API calls + S3 storage
- Agentcore: Lambda + Bedrock Agent + Bedrock API calls
Note: We use embedded context instead of Knowledge Bases to avoid OpenSearch Serverless costs (~$175/month).
π For detailed comparison, see BACKEND_COMPARISON.md
- Check
NEXT_PUBLIC_API_URLin frontend.env.local - Verify backend is running:
curl http://localhost:8000/docs - Check CORS configuration in
backend/server.py - Review browser console for exact error messages
- Verify Docker is running:
docker --version - Check AWS credentials:
aws sts get-caller-identity - Ensure Bedrock access:
aws bedrock list-foundation-models --region us-east-1 - Review Lambda package size (must be < 50MB for direct upload)
- Model IDs may require regional prefix (e.g.,
us.amazon.nova-lite-v1:0) - Ensure Bedrock is available in your AWS region
- Check IAM permissions for bedrock:InvokeModel
- Verify Terraform deployed successfully:
cd terraform && terraform output agentcore_agent_id - Check agent status in AWS Console: Bedrock > Agents
- Ensure agent is prepared:
./scripts/upload_knowledge_base.sh dev - Verify IAM permissions for bedrock:InvokeAgent
- Context is embedded during Terraform deployment from
backend/data/files - If you update facts/style/summary, run
terraform applyagain - Check agent instructions in AWS Console to verify context is present
- π Documentation Index:
DOCUMENTATION_INDEX.md- Complete guide to all docs - π³ Decision Tree:
DECISION_TREE.md- Quick decision guide - βοΈ Backend Comparison:
BACKEND_COMPARISON.md- Detailed feature comparison - π Agentcore Quick Start:
QUICK_START_AGENTCORE.md- Deploy in 3 steps - π Agentcore Migration Guide:
AGENTCORE_MIGRATION_GUIDE.md- Full deployment guide - π° Cost Optimization:
AGENTCORE_COST_OPTIMIZED.md- Cost breakdown & savings - π Changelog:
CHANGELOG.md- Version history
- Next.js Documentation
- FastAPI Documentation
- AWS Bedrock
- AWS Bedrock Agentcore
- Terraform AWS Provider
- Create a feature branch:
git checkout -b feature/my-feature - Make changes and test locally
- Commit with clear messages:
git commit -m "Add feature X" - Push and create a pull request
This project is private. All rights reserved.
Created by: mgbec
Repository: digtw
For issues or questions:
- Check the troubleshooting section above
- Review backend logs:
aws logs tail /aws/lambda/twin-dev-backend --follow - Check frontend browser console for client-side errors
- Review GitHub Issues or contact the maintainer
- β¨ Added AWS Bedrock Agentcore backend (parallel deployment)
- β¨ Dual backend architecture: Traditional + Agentcore
- β¨ Cost-optimized: No Knowledge Base (embedded context)
- β¨ Built-in conversation memory with Agentcore
- β¨ Multi-agent orchestration ready
- π Comprehensive Agentcore documentation
- π Traditional FastAPI backend with Bedrock
- π Next.js frontend with TypeScript
- π Terraform infrastructure as code
- π Multi-environment support
Last Updated: December 2025
Version: 0.2.0